<script setup>
import { ref } from "vue";
import { formatTime } from "@/utils/format";
import { Edit, Delete } from "@element-plus/icons-vue";
import { getBorrowList, deleteBorrowDel, postReturnBook } from "@/api/borrow";

// v-loading 是否加载
const loading = ref(false);

// 记录总条数
const total = ref(0);

// 请求参数
const params = ref({
  pagenum: 1,
  pagesize: 2,
  card: "", //借书卡
  bookName: "", //图书名称
  returnType: null, //还书状态
});

// 记录借阅信息
const borrowInfo = ref([
  {
    id: 10,
    bookName: "西游记",
    card: "8120116042",
    name: "张三",
    lendingTime: "2015-01-01 00:00:00",
    returnTime: "2015-01-01 00:00:00",
    status: 0, // 正常归还
  },
  {
    id: 10,
    bookName: "西游记",
    card: "8120116042",
    name: "张三",
    lendingTime: "2015-01-01 00:00:00",
    returnTime: "",
    status: 1, //在借中
  },
  {
    id: 10,
    bookName: "西游记",
    card: "8120116042",
    name: "张三",
    lendingTime: "2015-01-01 00:00:00",
    returnTime: "2015-01-01 00:00:00",
    status: 2, //2延迟还书
  },
  {
    id: 10,
    bookName: "西游记",
    card: "8120116042",
    name: "张三",
    lendingTime: "2015-01-01 00:00:00",
    returnTime: "",
    status: 3, //3丢失图书
  },
  {
    id: 10,
    bookName: "西游记",
    card: "8120116042",
    name: "张三",
    lendingTime: "2015-01-01 00:00:00",
    returnTime: "2015-01-01 00:00:00",
    status: 4, //4破损还书
  },
]);

// 借书
const borrowBook = () => {
  // console.log("借书");
  // 显示抽屉
  returnDrawerRef.value.switchDrawer();
};

// 控制消息对话框的显示
const dialogFormVisible = ref(false);
// 还书提交数据
const returnBook = ref({
  id: null,
  returnTime: "",
  status: null,
});

// 点击图标归还图书
const onReturnBook = (row) => {
  // 显示消息对话框
  dialogFormVisible.value = true;

  returnBook.value.id = row.id;
};
// 归还图书
const onSubmit = async () => {
  // 设置归还图书
  returnBook.value.returnTime = Date.now();
  const res = await postReturnBook(returnBook.value);
  // 关闭消息对话框
  dialogFormVisible.value = false;

  ElMessage.success("归还成功");
  // 刷新列表
  borrowList();
};
// 点击图标删除借阅记录
const onDelete = (row) => {
  //console.log("点击图标删除借阅记录", row);
  ElMessageBox.confirm("温馨提示！是否删除该条记录", "删除记录", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await deleteBorrowDel(row.id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      borrowList();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消成功",
      });
    });
};

// 重置
const reset = () => {
  params.value = {
    pagenum: 1,
    pagesize: 2,
    card: "", //借书卡
    bookName: "", //图书名称
    returnType: null, //还书状态
  };
  // 重新渲染
  borrowList();
};

// 分页
// 每页条数变化时的回调
const handleSizeChange = (val) => {
  //console.log(val)
  params.value.pagesize = val;
  borrowList();
};
// 当前页码变化时的回调
const handleCurrentChange = (val) => {
  params.value.pagenum = val;
  borrowList();
};

// 获取借阅列表
const borrowList = async () => {
  // 加载
  loading.value = true;
  const res = await getBorrowList(params.value);
  borrowInfo.value = res.data.data;
  total.value = res.data.total;
  // 结束加载
  loading.value = false;
};
// 初始化列表
borrowList();

// 获取子组件dom
const returnDrawerRef = ref(null);

// 重新刷新借阅列表;
const success = () => {
  //跳转到最后一页
  params.value.pagenum = Math.ceil(total.value / params.value.pagesize);
  borrowList();
};
</script>

<template>
  <Card title="借阅管理">
    <template #extra>
      <el-button type="success" @click="borrowBook">借书</el-button>
    </template>
    <el-form :inline="true">
      <el-form-item label="卡号">
        <el-input v-model="params.card" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="params.bookName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="还书类型">
        <el-select v-model="params.returnType" style="width: 100px">
          <el-option label="正常还书" :value="0" />
          <el-option label="在借中" :value="1" />
          <el-option label="延迟还书" :value="2" />
          <el-option label="丢失图书" :value="3" />
          <el-option label="破损还书" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="borrowList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="borrowInfo" style="width: 100%" v-loading="loading">
      <el-table-column label="序列" width="80" type="index" />
      <el-table-column prop="bookName" label="图书名单" />
      <el-table-column prop="card" label="借书卡" width="110" />
      <el-table-column prop="name" label="借阅人" />
      <el-table-column label="借阅时间" width="170">
        <template #default="{ row }">
          {{ formatTime(row.lendingTime) }}
        </template>
      </el-table-column>
      <el-table-column label="还书时间" width="170">
        <template #default="{ row }">
          {{ formatTime(row.returnTime) }}
        </template>
      </el-table-column>
      <el-table-column label="还书类型">
        <template #default="{ row }">
          <el-tag v-if="row.status === 0" type="success">正常还书</el-tag>
          <el-tag v-else-if="row.status === 1" type="primary">在借中</el-tag>
          <el-tag v-else-if="row.status === 2" type="info">延期还书</el-tag>
          <el-tag v-else-if="row.status === 3" type="danger">丢失图书</el-tag>
          <el-tag v-else-if="row.status === 4" type="warning">破损还书</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <!-- 归还图书 -->
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            v-if="row.status === 1 || row.status === 3"
            @click="onReturnBook(row)"
          >
          </el-button>
          <el-button :icon="Edit" circle plain disabled v-else> </el-button>

          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            v-if="row.status != 1 && row.status !== 3"
            @click="onDelete(row)"
          ></el-button>
          <el-button :icon="Delete" circle plain disabled v-else> </el-button>
        </template>
      </el-table-column>
      <!-- 空数据时 -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]"
      background
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </Card>

  <!-- 消息对话框 -->
  <el-dialog v-model="dialogFormVisible" title="归还图书" width="400">
    <el-form>
      <el-form-item label="请选择还书类型" :label-width="formLabelWidth">
        <el-select v-model="returnBook.status">
          <el-option label="正常还书" :value="0" />
          <el-option label="延迟还书" :value="2" />
          <el-option label="丢失图书" :value="3" />
          <el-option label="破损还书" :value="4" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 抽屉 -->
  <ReturnDrawer ref="returnDrawerRef" @success="success" />
</template>

<style scoped></style>
